package com.http.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.http.pojo.User;
import com.http.service.UserService;
import com.http.utils.HttpClient;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    public static final Logger log = LoggerFactory.getLogger(UserServiceImpl.class);

    @Value("${com.http.link:}")
    private String httpUrl;

    @Override
    public String getUserById(int id) throws IOException{
        HttpClient httpClient = new HttpClient();
        StringBuffer requestUrl = new StringBuffer();
        requestUrl.append(httpUrl).append("/queryUserById/").append(id);
        String response = httpClient.get(requestUrl.toString());
        log.info("response: "+response);
        JSONObject responseBody = JSON.parseObject(response);
        log.info("responseBody: "+responseBody);
        String message = responseBody.getString("message");
        log.info("message: "+message);
        String user = responseBody.getString("object");
        log.info("user: "+user);
        return user;
    }

    @Override
    public String updateUser(int id, String token, User user) throws IOException{
        HttpClient httpClient = new HttpClient();
        StringBuffer requestUrl = new StringBuffer();
        requestUrl.append(httpUrl).append("/updateUser/").append(id);
        Map<String, String> headMap =new HashMap<>();
        headMap.put("token",token);
        Headers setHeaders = httpClient.SetHeaders(headMap);
        RequestBody body = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), JSON.toJSONString(user));
        String response = httpClient.post(requestUrl.toString(),setHeaders,body);
        log.info("response: "+response);
        JSONObject responseBody = JSON.parseObject(response);
        log.info("responseBody: "+responseBody);
        String message = responseBody.getString("message");
        log.info("message: "+message);
        String object = responseBody.getString("object");
        log.info("object: "+object);
        return object;
    }

    @Override
    public String createUser(User user) throws IOException{
        HttpClient httpClient = new HttpClient();
        StringBuffer requestUrl = new StringBuffer();
        requestUrl.append(httpUrl).append("/createUser");
        RequestBody body = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), JSON.toJSONString(user));
        String response = httpClient.post(requestUrl.toString(),body);
        log.info("response: "+response);
        JSONObject responseBody = JSON.parseObject(response);
        log.info("responseBody: "+responseBody);
        String message = responseBody.getString("message");
        log.info("message: "+message);
        String object = responseBody.getString("object");
        log.info("object: "+object);
        return object;
    }


}
